package com.oa.controller.user;

import com.oa.service.user.FileUploadService;
import com.oa.vo.FileUploadVo;
import com.oa.vo.SysResult;
import com.qiniu.common.QiniuException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author: 一拳超人
 * @Date: 2021/10/10 15:26
 */
@CrossOrigin
@RestController
@RequestMapping("/user/file")
public class FileUploadController {
    @Autowired
    private FileUploadService fileUploadService;

    @PostMapping("/empImagesUpload")
    public SysResult uploadFile(MultipartFile file) throws IOException {
        FileUploadVo fileUploadVo = fileUploadService.uploadFile(file.getInputStream());
        if (fileUploadVo.getUrlPath().equals("201")) {
            return SysResult.fail();
        }
        return SysResult.success(fileUploadVo);
    }

    @DeleteMapping("/deleteFile")
    public SysResult deleteFile(String key) throws QiniuException {
        key = key.substring(key.lastIndexOf("/") + 1);
        Integer status = fileUploadService.deleteUploadFile(key);
        if (status != 200) {
            return SysResult.fail(status);
        }
        return SysResult.success(status);
    }
}
